﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ARR.UI.Models;
using System.Windows.Input;
using ARR.Shared;
using System.Collections.ObjectModel;
using ARR.Entities;
using ARR.UI.Commands;

namespace ARR.UI.ViewModels
{
    public class CategoryMatchMasterViewModel : WorkspaceViewModel
    {
        private ICategoryMatchMasterModel model;
        private ICommand saveCommand;
        private ICommand cancelCommand;
        private ICommand deleteCommand;



        public CategoryMatchMasterViewModel(ICategoryMatchMasterModel model)
        {
            this.DisplayName = Labels.MargenesTeoricos;
            this.model = model;
        }
        


        public ICommand SaveCommand
        {
            get
            {
                if (saveCommand == null)
                {
                    saveCommand = new RelayCommand(e => Save());
                }
                return saveCommand;
            }
        }

        public ICommand CancelCommand
        {
            get
            {
                if (cancelCommand == null)
                {
                    cancelCommand = new RelayCommand(e => Cancel());
                }
                return cancelCommand;
            }
        }

        public ICommand DeleteCommand
        {
            get
            {
                if (deleteCommand == null)
                {
                    deleteCommand = new RelayCommand(e => Delete());
                }
                return deleteCommand;
            }
        }

        public ObservableCollection<Category> Categories
        {
            get
            {
                return model.Categories;
            }
        }

        public ObservableCollection<CategoryMatch> NacsCategoryMatches
        {
            get
            {
                return model.NacsCategoryMatches;
            }
        }

        public ObservableCollection<CategoryMatch> OtherCatogoryMatches
        {
            get
            {
                return model.OtherCatogoryMatches;
            }
        }


        private void Save()
        {
            if (AreValid())
            {
                model.Save();

                model.LoadCategoryMatches();

                OnPropertyChanged<CategoryMatchMasterViewModel>(p => p.NacsCategoryMatches);
                OnPropertyChanged<CategoryMatchMasterViewModel>(p => p.OtherCatogoryMatches);

            }
        }

        private void Cancel()
        {
            model.LoadCategoryMatches();

            OnPropertyChanged<CategoryMatchMasterViewModel>(p => p.NacsCategoryMatches);
            OnPropertyChanged<CategoryMatchMasterViewModel>(p => p.OtherCatogoryMatches);

        }

        private void Delete()
        {

        }

        private bool AreValid()
        {
            foreach (CategoryMatch match in NacsCategoryMatches.Union(OtherCatogoryMatches))
            {
                if (!match.IsValid)
                    return false;
            }

            return true;
        }

    }
}
